package com.example.demo1;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello,Spring boot";
    }

    @RequestMapping("/r1")
    public String r1(String name){
        return "接到参数, name:"+name;
    }

    @RequestMapping("/r2")
    public String r2(Integer age){
        return "接到参数, age:"+age;
    }

    @RequestMapping("/r3")
    public String r3(int age){
        return "接到参数, age:"+age;
    }

    @RequestMapping("/r4")
    public String r4(String name,Integer age){
        return "接到参数, name:"+name+", age:"+age;
    }

    @RequestMapping("/r5")
    public String r5(Student student){
        return "接到参数, student:"+student;
    }

    @RequestMapping("/r6")
    public String r6(@RequestParam("name") String userName){
        return "接到参数, name:"+userName;
    }


    @RequestMapping("/r7")
    public String r7(@RequestParam(value = "name",required = false) String userName){
        return "接到参数, name:"+userName;
    }


    /**
     * 接收数组
     * @param arr
     * @return
     */
    @RequestMapping("/r8")
    public String r8(String[] arr){
        return "接到参数, arr:"+ Arrays.toString(arr);
    }

    /**
     * 接收集合
     * @param list
     * @return
     */

    @RequestMapping("/r9")
    public String r9(@RequestParam List<String> list){
        return "接到参数, arr:"+list;
    }

    @RequestMapping("/r10")
    public String r10(@RequestBody Student student){
        return "接到参数, student:"+student;
    }

    /**
     * 从路径中获取参数
     * @param articId
     * @return
     */
    @RequestMapping("/article/{articId}")
    public String r10(@PathVariable("articId") Integer articId){
        return "接到参数, articId:"+articId;
    }

    /**
     * 从路径中获取参数
     * @param articId
     * @return
     */
    @RequestMapping("/article/{articId}/{name}")
    public String r10(@PathVariable("articId") Integer articId,@PathVariable("name") String name){
        return "接到参数, articId:"+articId+","+"name:"+name;
    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @RequestMapping("/r12")
    public String r12(MultipartFile file){
        String originalFilename= file.getOriginalFilename();
        return "接收到文件，文件名称:"+originalFilename;
    }

    /**
     * 上传图片
     * @param imgfile
     * @return
     */
    @RequestMapping("/r13")
    public String r13(@RequestPart("file") MultipartFile imgfile){
        String originalFilename= imgfile.getOriginalFilename();
        return "接收到文件，文件名称:"+originalFilename;
    }

}
